﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.DirectShow;
using System.Windows.Media.DirectShow.Filters;
using System.Windows.Media.DirectShow.UI;
using System.Windows.Media.DirectShow.Ogg;


namespace Test
{
    public partial class OggPage : UserControl
    {
        private FilterGraph graph;
        private WaveWriter writer;

        private OggVorbisDecoder sourceFilter;
        private VideoAudioRenderer videoAudioRenderer;
        private AudioVisualization audioVisualization;

        public OggPage()
        {
            InitializeComponent();

            graph = new FilterGraph();

            videoAudioRenderer = new VideoAudioRenderer();
            graph.AddFilter("render", videoAudioRenderer);

            sourceFilter = new OggVorbisDecoder();
            sourceFilter.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler(sourceFilter_LoadCompleted);
            sourceFilter.LoadAsync(new Uri("test1.ogg", UriKind.Relative));
            graph.AddFilter("source", sourceFilter);

            BarVisualization visualization = new BarVisualization();
            audioVisualization = new AudioVisualization(visualization);
            this.v.Child = audioVisualization.UIElement;
            graph.AddFilter("av", audioVisualization);
        }

        void sourceFilter_LoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            graph.ConnectFilters(sourceFilter, audioVisualization);
            graph.ConnectFilters(audioVisualization, videoAudioRenderer);

            graph.RunAsync();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            graph.StopAsync();
        }

        private void Pause_Click(object sender, RoutedEventArgs e)
        {
            if (graph.State != FilterState.Paused)
                graph.PauseAsync();
            else
                graph.RunAsync();
        }


        private void Play_Click(object sender, RoutedEventArgs e)
        {
            graph.RunAsync();
        }

    }
}
